package com.attilax.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteException;
import org.apache.commons.exec.PumpStreamHandler;
import org.junit.Test;

import com.attilax.data.csv.csvService;
import com.attilax.io.FileUtilsAti;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
@SuppressWarnings("all")
public class shellUtil {

	public static List<Map> parse(String result) {
		List<Map> tab =toTableNoHeadMode_ByMultiSpace(result);
				//csvService.toTableByTab(result);;
		return 	tab;
	}
	
	
	public static List<Map> toTableNoHeadMode_ByMultiSpace(String csv) {
		List<Map> li = Lists.newArrayList();
		String[] rows = csv.split("\n");
		int idx = 0;
		for (String row : rows) {
			row=row.trim();
			if(row.length()==0)
				continue;
			Map m = Maps.newLinkedHashMap();
			String[] cols = row.split(" ");
			
			List<String> cols_li=getcols_li(cols);
			for (int j = 0; j < cols_li.size(); j++) {

				m.put("index" + String.valueOf(j), cols_li.get(j));

			}
			li.add(m);

		}
		return li;
	}
	
	private static String exec(String cmd) throws ExecuteException, IOException, UnsupportedEncodingException {
		final CommandLine cmdLine = CommandLine.parse(cmd);
		DefaultExecutor executor = new DefaultExecutor();

		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		executor.setStreamHandler(new PumpStreamHandler(baos, baos));// iytstren   std,err stream
	//	executor.set
		executor.execute(cmdLine);

		String result = baos.toString("gbk").trim();
		return result;
	}
	private static List<String> getcols_li(String[] cols) {
 
		List<String> stringA = Arrays.asList(cols);
		List<String>  li=	Listutil.delEmptyElement(stringA);
		return li;
	}

	@Test
	public void test_parse_netstatNfindstr()
	{
		String f="C:\\ptln.txt";
		String t=FileUtilsAti.readFileToStringAutoDetectEncode(f);
		List<Map> parse_netstatNfindstr = shellUtil.parse_netstatNfindstr_linux(t);
		System.out.println(parse_netstatNfindstr);
	}
	
	public static List<Map> parse_netstatNfindstr_linux(String result) {
		List<Map> tab =toTableNoHeadMode_netstat(result);
	//	tab.remove(0);
		
		return tab;
	}


	
	private static List<Map> toTableNoHeadMode_netstat(String csv) {
		List<Map> li = Lists.newArrayList();
		String[] rows = csv.split("\n");
		String titleline=rows[1];
		int Local_Address_idex=titleline.indexOf("Local Address");
		int Foreign_Address=titleline.indexOf("Foreign Address");
		int PIDProgram_name_idx=titleline.indexOf("PID/Program name");
		
		int idx = 0;
		for (String row : rows) {
			idx++;
			if(idx<=2)
				continue;
			row=row.trim();
			if(row.length()==0)
				continue;
			Map m = Maps.newLinkedHashMap();
			 m.put("Local Address", row.substring(Local_Address_idex, Foreign_Address));
			 String piddtr = row.substring(PIDProgram_name_idx);
			m.put("PIDProgram name", piddtr);
			m.put("PID", piddtr.split("/")[0]);
			li.add(m);

		}
		return li;
	}


	public static List<Map> parse_netstatNfindstr(String result) {
		List<Map> tab =toTableNoHeadMode_ByMultiSpace(result);
		tab.remove(0);
		
		return csvService.toTableBySpace_firstlineTitle(tab);
	}

}
